Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternate solution to command api. #446

Merged
merged 71 commits into from
Oct 26, 2023
Merged

Conversation

JackCrumpLeys
Copy link
Contributor

@JackCrumpLeys JackCrumpLeys commented Aug 6, 2023

Objective

I want to add commands ergonomically, unfortunately @Jenya705 's implementation did not look to promising for my purposes so I am working on an alternative command api.

Solution

lib-users should be able to make commands quickly and easily.

Some objectives in the order I plan to implement them:

  • Sync commands to players based on command visibility scope.
  • Derive Macro
  • Execute and handle commands.
  • Parse commands with a lot of lib-user friendly options.
  • Handle server-side suggestions. (this has been defered to the future)

@JackCrumpLeys JackCrumpLeys marked this pull request as draft August 6, 2023 04:27
@JackCrumpLeys JackCrumpLeys changed the title Anternate solution to command api. Alternate solution to command api. Aug 6, 2023
Copy link
Member

@rj00a rj00a left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly skimmed through it for now but it's looking good so far. Let me know when you're ready for another review.

crates/valence_command/src/arg_parser.rs Outdated Show resolved Hide resolved
crates/valence_command/src/arg_parser.rs Outdated Show resolved Hide resolved
crates/valence_command/src/arg_parser.rs Outdated Show resolved Hide resolved
crates/valence_command/src/arg_parser.rs Outdated Show resolved Hide resolved
crates/valence_command/src/arg_parser.rs Outdated Show resolved Hide resolved
crates/valence_command/src/arg_parser.rs Outdated Show resolved Hide resolved
crates/valence_command/src/arg_parser.rs Outdated Show resolved Hide resolved
crates/valence_command/src/command_graph.rs Outdated Show resolved Hide resolved
crates/valence_command/src/command_scopes.rs Outdated Show resolved Hide resolved
crates/valence_command/src/manager.rs Outdated Show resolved Hide resolved
crates/valence_command/src/arg_parser.rs Outdated Show resolved Hide resolved
crates/valence_command/src/command_graph.rs Outdated Show resolved Hide resolved
crates/valence_command/src/command_scopes.rs Outdated Show resolved Hide resolved
crates/valence_command/src/command_scopes.rs Outdated Show resolved Hide resolved
examples/command.rs Outdated Show resolved Hide resolved
crates/valence_command/src/handler.rs Show resolved Hide resolved
crates/valence_command/src/handler.rs Outdated Show resolved Hide resolved
examples/command.rs Outdated Show resolved Hide resolved
crates/valence_command/src/lib.rs Outdated Show resolved Hide resolved
examples/command.rs Outdated Show resolved Hide resolved
examples/command.rs Outdated Show resolved Hide resolved
examples/command.rs Show resolved Hide resolved
crates/valence_command/src/handler.rs Outdated Show resolved Hide resolved
Copy link
Member

@rj00a rj00a Sep 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing that's missing is error handling for commands that failed to parse. If someone types /gamemode aklwjdlkawlhd, How do I tell them Unknown game mode: aklwjdlkawlhd?

I also noticed that in vanilla, /gamemode aklwjdlkawlhd will give me the "unknown game mode" message while the command is still being typed. What packet is responsible for that, and how do we do that ourselves? It's because the gamemode parser isn't being used in this example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is up to whomever handles raw strings to send warnings (the client will show red text with the current parsing). Trying to guess what node a client is talking about with a broken or incomplete command is a bad idea.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But how do I get access to the error that occurred so I can send an error in the chat or whatever else? It looks like the error is thrown away.

https://github.com/valence-rs/valence/pull/446/files#diff-494de62f0e3a6fa84f8d7e1d109af30f30d37abba15f0271dff866d8512c20c1R502-R503

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing is when trying to match a node an error is assumed to mean that the command doesn't match the given node. For example if we have two executables at /foo and one is a number arg and the other is a string arg /foo bar should not trigger "expected number got bar".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a look at the source for GameModeArgumentType and it throws an exception when the gamemode fails to parse.

private static final DynamicCommandExceptionType INVALID_GAME_MODE_EXCEPTION = new DynamicCommandExceptionType(gameMode -> Text.translatable("argument.gamemode.invalid", gameMode));

argument.gamemode.invalid translates to the message the user sees when they type /gamemode aklwjdlkawlhd.

So clearly the error is bubbling up somehow, but I don't know what the semantics are exactly. Might need to dig into the brigadier source.

Copy link
Contributor Author

@JackCrumpLeys JackCrumpLeys Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would have to make some assumptions 1. no other nodes match 2. no better fit for error reporting. This applies fine to the small set of uncomplicated vinnila commands but anything more complex and dynamic will cause headaches in the future. OTOH We could label exclusive args somehow (the only arg reachable at a certain path) and bubble those errors up.

Copy link
Member

@rj00a rj00a Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe vanilla is throwing away all but the last exception? Just a guess, but I'd like to get a confirmation so we can have more confidence in a decision.

Copy link
Contributor Author

@JackCrumpLeys JackCrumpLeys Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we hash args based on the path taken to get to them and if the hash is unique then we report errors when the arg does not match. This only solves the problem for unique beaches (most cases). This would not be too hard to implement, I can probably do it in about 30m to an hour tomorrow.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But what is brigadier doing to resolve this situation? I doubt it's anything like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not read brigadier's code and I am not well versed on java so I don't particularly want to track it down.

crates/valence_command/src/handler.rs Outdated Show resolved Hide resolved
crates/valence_command/src/parsers.rs Outdated Show resolved Hide resolved
crates/valence_command/src/parsers.rs Outdated Show resolved Hide resolved
crates/valence_command/src/handler.rs Outdated Show resolved Hide resolved
examples/command.rs Outdated Show resolved Hide resolved
#[derive(Command, Debug, Clone)]
#[paths("gamemode", "gm")]
#[scopes("valence.command.gamemode")]
enum Gamemode {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this is the best command to use as an example because people like me will be wondering why we're not just using the normal GameMode parser.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like how the current version shows multiple paths or redirections. we could add a comment if we must.

crates/valence_command/src/parsers.rs Outdated Show resolved Hide resolved
crates/valence_command/src/parsers.rs Outdated Show resolved Hide resolved
crates/valence_command/src/parsers.rs Outdated Show resolved Hide resolved
crates/valence_command/src/parsers.rs Outdated Show resolved Hide resolved
crates/valence_command/src/parsers.rs Show resolved Hide resolved
@rj00a rj00a enabled auto-merge (squash) October 26, 2023 01:11
@rj00a rj00a merged commit f43e60e into valence-rs:main Oct 26, 2023
11 checks passed
@JackCrumpLeys JackCrumpLeys mentioned this pull request Oct 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants